package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"choujiang/box_server/service"
	"choujiang/common/consts"
	"choujiang/common/models"
	"choujiang/common/utils"
	"strconv"
)

/**
 @description 生成礼券信息
 */
func CreateCoupon(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	dlId := form.Get("dlId")
	giftId := form.Get("giftId")
	startAt := form.Get("startAt")
	endAt := form.Get("endAt")
	if uniqueId == "" || dlId == "" || giftId == "" || startAt == "" || endAt == "" {
		utils.Error(consts.ErrInvalidParams, c)
		return
	}
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	intDlId, _ := strconv.ParseInt(dlId, 10, 64)
	intGiftId, _ := strconv.ParseInt(giftId, 10, 64)
	intStartAt, _ := strconv.ParseInt(startAt, 10, 64)
	intEndAt, _ := strconv.ParseInt(endAt, 10, 64)
	coupon, err := service.CreateCoupon(user.Id, intDlId, intGiftId, intStartAt, intEndAt, uniqueId)
	if err != nil {
		fmt.Printf("CreateCoupon err: %s", err)
		utils.Error(consts.ErrInterval, c)
		return
	}

	utils.JSON(coupon, c)
}

/**
 @description 参与抽奖活动
 */
func JoinCoupon(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	goodsId := form.Get("goodsId")
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	number := form.Get("number")
	number64, _ := strconv.ParseInt(number, 10, 64)
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	userExtra := model.GetUserExtraByUid(user.Id)
	if number64 <= 0 || number64 > userExtra.Coupon {
		utils.Error(consts.ErrCouponNumberIncorrect, c)
		return
	}
	goods := model.GetGoodsById(goodsId64)
	if goods == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	if goods.CouponStatus == 1 {
		utils.Error(consts.ErrCouponFinished, c)
		return
	}
	service.JoinCoupon(user, userExtra, goods, number64)

	utils.JSON("ok", c)
}

/**
 @description 抽奖详情
 */
func CouponDetail(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	goodsId := form.Get("goodsId")
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	good := model.GetGoodsById(goodsId64)
	if good == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	ret := make(map[string]interface{})
	goods, err := service.GetGoodsDetail(goodsId64)
	if err != nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	ret["goods"] = goods
	joinDetail := service.MyParticipateDetail(user.Id, goods.Id)
	ret["join_detail"] = joinDetail
	winner, _ := service.GetCouponMember(good)
	ret["winner"] = winner
	couponSource := service.MyCouponSource(user.Id, good.Id)
	ret["coupon_source"] = couponSource

	utils.JSON(ret, c)
}

/**
 @description 我的抽奖记录
 */
func MyCouponLog(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	pageNo := form.Get("pageNo")
	pageSize := form.Get("pageSize")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	pageNoInt, _ := strconv.Atoi(pageNo)
	pageSizeInt, _ := strconv.Atoi(pageSize)
	couponLog := service.GetMyCouponLog(user, pageNoInt, pageSizeInt)

	utils.JSON(couponLog, c)
}

/**
 @description 我的抽奖详情
 */
func MyCouponDetail(c *gin.Context) {
	form := c.Request.Form
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	goods := model.GetGoodsById(goodsId64)
	if goods == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	ret := make(map[string]interface{})
	ret["goods"] = service.GetMyCouponDetail(user, goods)
	winner, member := service.GetCouponMember(goods)
	ret["winner"] = winner
	ret["member"] = member
	ret["soon_lottery"] = service.GetSoonCoupon(goodsId64)

	utils.JSON(ret, c)
}

/**
 @description 获取本次抽奖的用户信息接口
 */
func CouponMember(c *gin.Context) {
	form := c.Request.Form
	pageNo := form.Get("pageNo")
	pageSize := form.Get("pageSize")
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	goods := model.GetGoodsById(goodsId64)
	if goods == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	int64PageNo, _ := strconv.ParseInt(pageNo, 10, 64)
	int64PageSize, _ := strconv.ParseInt(pageSize, 10, 64)
	member := service.GoodsMembers(goods, int64PageNo, int64PageSize)

	utils.JSON(member, c)
}

/**
 @description 刷新用户的通知form_id
 */
func RefreshForm(c *gin.Context) {
	form := c.Request.Form
	formId := form.Get("formId")
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	goods := model.GetGoodsById(goodsId64)
	if goods == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	service.RefreshUserFormId(user, goods, formId)

	utils.JSON("ok", c)
}

//FreeNum: 是否参与过免费抽奖
func FreeNumHandler(c *gin.Context) {
	form := c.Request.Form
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	num := service.FreeNumService(user, goodsId)

	utils.JSON(num, c)
}

//FreeHandler: 参与免费抽奖
func FreeHandler(c *gin.Context) {
	form := c.Request.Form
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	err := service.FreeLotteryService(user, goodsId)
	if err != nil {
		utils.Error(consts.ErrInterval, c)
		return
	}
	utils.JSON("ok", c)
}
